package org.dromara.web.service.business;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.dao.business.domain.SmartAgentCategory;
import org.dromara.dao.business.domain.SmartAgentCategoryI18n;
import org.dromara.dao.business.domain.SmartAgentCategoryQuery;
import org.dromara.dao.business.mapper.SmartAgentCategoryI18nMapper;
import org.dromara.dao.business.mapper.SmartAgentCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class SmartAgentCategoryService {

    @Autowired
    SmartAgentCategoryMapper smartAgentCategoryMapper;
    @Autowired
    SmartAgentCategoryI18nMapper smartAgentCategoryI18nMapper;


    public TableDataInfo<SmartAgentCategory> queryPageList(SmartAgentCategoryQuery query, PageQuery pageQuery) {
        return new TableDataInfo(
                smartAgentCategoryMapper.paginationSelectList(query, pageQuery),
                smartAgentCategoryMapper.paginationSelectCount(query)
        );
    }


    public SmartAgentCategory queryById(Long id) {
        return smartAgentCategoryMapper.selectById(id);
    }


    @Transactional
    public SmartAgentCategory insert(SmartAgentCategory entity) {
        smartAgentCategoryMapper.insert(entity);
        return entity;
    }

    @Transactional
    public void updateById(SmartAgentCategory update) {

        SmartAgentCategory entity = smartAgentCategoryMapper.selectById(update.getId());
        if (entity == null) {
            throw new ServiceException();
        }
        entity.setName(update.getName());
        entity.setStatus(update.getStatus());
        entity.setUpdateTime(LocalDateTime.now());
        smartAgentCategoryMapper.updateById(entity);
    }


    @Transactional
    public void deleteById(Long id) {
        smartAgentCategoryMapper.deleteById(id);
    }

    @Transactional
    public void deleteByIds(Long[] ids) {
        smartAgentCategoryMapper.deleteByIds(Arrays.asList(ids));
    }



    public List<SmartAgentCategory> queryAll() {
        return smartAgentCategoryMapper.selectList(new LambdaQueryWrapper<>());
    }


    public List<SmartAgentCategory> getValidCategory(String lang) {
//        return smartAgentCategoryMapper.selectList(new LambdaQueryWrapper<SmartAgentCategory>().eq(SmartAgentCategory::getStatus, true));
        return smartAgentCategoryMapper.selectCategory(lang);
    }


    public List<SmartAgentCategoryI18n> listI18n(Long categoryId) {
        return smartAgentCategoryI18nMapper.selectList(new LambdaQueryWrapper<SmartAgentCategoryI18n>().eq(SmartAgentCategoryI18n::getCategoryId, categoryId));
    }

    public void addI18n(SmartAgentCategoryI18n entity) {
        smartAgentCategoryI18nMapper.insert(entity);
    }

    public void updateI18n(SmartAgentCategoryI18n update) {
        SmartAgentCategoryI18n i18n = smartAgentCategoryI18nMapper.selectById(update.getId());
        i18n.setName(update.getName());
        i18n.setLang(update.getLang());
        i18n.setCategoryId(update.getCategoryId());
        smartAgentCategoryI18nMapper.updateById(i18n);
    }

    public void deleteI18n(Long categoryId) {
        smartAgentCategoryI18nMapper.deleteById(categoryId);
    }
}


